#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, k, ans;
int f[N], d[N];
void init() { rep(i, 1, n) f[i] = i, d[i] = 0; }
int find(int x) {
  if (f[x] == x) return x;
  int y = f[x];
  f[x] = find(f[x]);
  d[x] = (d[x] + d[y]) % 3;
  return f[x];
}
bool merge(int x, int y, int dis) {
  if (x > n || y > n) return false;
  int fx = find(x), fy = find(y);
  if (fx == fy) return (d[x] + dis) % 3 == d[y];
  f[fy] = fx;
  d[fy] = (d[x] + dis - d[y] + 3) % 3;
  return true;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  init();
  rep(i, 1, k) {
    int op, u, v;
    cin >> op >> u >> v;
    if (!merge(u, v, op - 1)) ans++;
  }
  cout << ans;
  return 0;
}